package com.wh.custom.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.utils.ExcelUtils;
import com.wh.basic.vo.AjaxResult;
import com.wh.custom.dto.CustomSalesmanQueryDto;
import com.wh.custom.pojo.CustomSalesman;
import com.wh.custom.service.ICustomSalesmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/customSalesman")
public class CustomSalesmanController {

        @Autowired
        public ICustomSalesmanService customSalesmanService;

        @PutMapping
        public AjaxResult addOrUpdate(@RequestBody CustomSalesman customCustomSalesman) {
            if ( customCustomSalesman.getId() != null){
                customSalesmanService.updateById(customCustomSalesman);
            }else{
                customSalesmanService.save(customCustomSalesman);
            }
            return new AjaxResult();
        }

        /**
         * 接口：删除
         * @param id
         * @return AjaxResult 响应给前端
         */
        @DeleteMapping(value = "/{id}")
        public AjaxResult del(@PathVariable("id") Long id) {
            customSalesmanService.removeById(id);
            return new AjaxResult();
        }

        /**
         * 接口：批量删除
         * @param ids
         * @return AjaxResult 响应给前端
         */
        @PatchMapping
        public AjaxResult patchDel(@RequestBody List<Long> ids) {
            customSalesmanService.removeByIds(ids);
            return new AjaxResult();
        }

        /**
         * 接口：查询单个对象
         * @param id
         */
        @GetMapping("/{id}")
        public CustomSalesman findOne(@PathVariable("id") Long id) {
            return customSalesmanService.getById(id);
        }


        /**
         * 接口：查询所有
         * @return
         */
        @GetMapping
        public List<CustomSalesman> findAll() {
            return customSalesmanService.list();
        }


        /**
         * 接口：分页查询或高级查询
         * @param
         * @return IPage<CustomCustomerlabel> 分页对象
         */
        @PostMapping
        public IPage<CustomSalesman> findByPage(@RequestBody CustomSalesmanQueryDto customSalesmanQueryDto) {
            return customSalesmanService.findByPage(customSalesmanQueryDto);
        }
    /*@GetMapping("/export")
    public void export( HttpServletResponse response){
        List<CustomSalesman> saleaman = customSalesmanService.list();
        //ExcelUtils.exportExcel(导出的数据, 标题, 页名称, 导出的类型, 文件名, 响应对象)
        //文件名如果不写后缀默认是xls，可以写上xlsx就是指定的xlsx类型
        ExcelUtils.exportExcel(saleaman, "业务员客户表", "业务员客户信息", CustomSalesman.class, "业务员客户表.xls", response);
    }*/
    }


